/******************************************************************************


TransformMatrix.as


Matrix helpers for drawing api


Christopher Thilgen - cthilgen@macromedia.com


September 12, 2001


CONFIDENTIAL AND PROPRIETARY INFORMATION.  The entire contents of this file
is Copyright (C) Macromedia, Inc. 1993-2000, All Rights Reserved.  This
document is an unpublished trade secret of Macromedia, Inc. and may not be
viewed, copied or distributed by anyone, without the specific, written
permission of Macromedia, Inc. 


******************************************************************************/


/*
- --           --
|  a   b   c  |
|  d   e   f  |
|  g   h   i  |
- --           --
*/


function TransformMatrix()
{       
        this.empty = true;
}


TransformMatrix.prototype.isEmpty = function()
{
        return this.empty;
}


TransformMatrix.prototype.init = function(a,b,d,e,g,h)
{
        this.a = a;
        this.d = d;
        this.g = g;
        this.b = b;
        this.e = e;
        this.h = h;
        this.c = 0;
        this.f = 0;
        this.i = 1;
        this.empty = false;
}

TransformMatrix.prototype.concat = function(m)
{
        if ( this.isEmpty() )
        {
                this.a = m.a;
                this.d = m.d;
                this.g = m.g;
                this.b = m.b;
                this.e = m.e;
                this.h = m.h;
                this.c = m.c;
                this.f = m.f;
                this.i = m.i;
        }
        else
        {
                var result = new TransformMatrix;
                result.a = this.a * m.a + this.b * m.d + this.c * m.g;
                result.b = this.a * m.b + this.b * m.e + this.c * m.h;
                result.c = this.a * m.c + this.b * m.f + this.c * m.i;
                result.d = this.d * m.a + this.e * m.d + this.f * m.g;
                result.e = this.d * m.b + this.e * m.e + this.f * m.h;
                result.f = this.d * m.c + this.e * m.f + this.f * m.i;
                result.g = this.g * m.a + this.h * m.d + this.i * m.g;
                result.h = this.g * m.b + this.h * m.e + this.i * m.h;
                result.i = this.g * m.c + this.h * m.f + this.i * m.i;
                this.a = result.a;
                this.d = result.d;
                this.g = result.g;
                this.b = result.b;
                this.e = result.e;
                this.h = result.h;
                this.c = result.c;
                this.f = result.f;
                this.i = result.i;
        }
        this.empty = false;
}


TransformMatrix.prototype.scale = function(sx,sy)
{
        var m = new TransformMatrix;
        m.init( sx, 0, 0, sy, 0, 0 );
        return( this.concat(m) );
}


TransformMatrix.prototype.translate = function(tx,ty)
{
        var m = new TransformMatrix;
        m.init( 1, 0, 0, 1, tx, ty );
        return ( this.concat(m) );
}


TransformMatrix.prototype.rotate = function(r)
{
        // r is in degrees - must convert to radians
        var rad = ( r / 180 ) * Math.PI;
        var m = new TransformMatrix;
        var cosVal = Math.cos(rad);
        var sinVal = Math.sin(rad);
        m.init( cosVal, sinVal, -sinVal, cosVal, 0, 0 );
        return( this.concat(m) );
}

TransformMatrix.prototype.shear = function(rx, ry)
{
        // r is in degrees - must convert to radians
        var radx = ( rx / 180 ) * Math.PI;
        var rady = ( ry / 180 ) * Math.PI;
        var m = new TransformMatrix;
        m.init( 1, rady, radx, 1, 0, 0 );
        return( this.concat(m) );
}

TransformMatrix.prototype.skew = function(r)
{
        // r is in degrees - must convert to radians
        var rad = ( r / 180 ) * Math.PI;
        var m = new TransformMatrix;
        var tanVal = Math.tan(rad);
        m.init( 1, 1/tanVal, 0, 1, 0, 0 );
        return( this.concat(m) );
}

